package top.zlcode.friend.service.user.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import top.zlcode.common.core.context.UserContextHelper;
import top.zlcode.common.core.domain.TableDataInfo;
import top.zlcode.common.core.enums.ExamListType;
import top.zlcode.common.core.enums.ResultCode;
import top.zlcode.common.security.exception.ServiceException;
import top.zlcode.common.security.service.TokenService;
import top.zlcode.friend.domain.exam.Exam;
import top.zlcode.friend.domain.exam.dto.ExamDTO;
import top.zlcode.friend.domain.exam.dto.ExamQueryDTO;
import top.zlcode.friend.domain.exam.vo.ExamVO;
import top.zlcode.friend.domain.user.UserExam;
import top.zlcode.friend.manager.ExamCacheManager;
import top.zlcode.friend.mapper.exam.ExamMapper;
import top.zlcode.friend.mapper.user.UserExamMapper;
import top.zlcode.friend.service.user.IUserExamService;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserExamServiceImpl extends ServiceImpl<UserExamMapper, UserExam> implements IUserExamService {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ExamMapper examMapper;

    @Autowired
    private ExamCacheManager examCacheManager;

    @Value("${jwt.secret}")
    private String secret;

    @Override
    public int enter(ExamDTO examDTO) {

        Exam exam = examMapper.selectById(examDTO.getExamId());
        if (exam == null) {
            throw new ServiceException(ResultCode.EXAM_NOT_EXISTS);
        }

        if (exam.getStartTime().isBefore(LocalDateTime.now())) {
            throw new ServiceException(ResultCode.EXAM_STARTED);
        }

        Long userId = UserContextHelper.getUserId();
        UserExam userExam = baseMapper.selectOne(new LambdaQueryWrapper<UserExam>()
                .eq(UserExam::getExamId, examDTO.getExamId())
                .eq(UserExam::getUserId, userId));
        if (userExam != null) {
            throw new ServiceException(ResultCode.EXAM_HAS_ENTER);
        }

        examCacheManager.addUserExamCache(userId, examDTO.getExamId());
        userExam = new UserExam();
        userExam.setExamId(examDTO.getExamId());
        userExam.setUserId(userId);
        return baseMapper.insert(userExam);
    }

    @Override
    public TableDataInfo getList(ExamQueryDTO examQueryDTO) {
        Long userId = UserContextHelper.getUserId();
        Long total = examCacheManager.getListSize(ExamListType.USER_EXAM_LIST.getValue(), userId);
        List<ExamVO> examVoList;
        if (total == null || total == 0) {
            PageHelper.startPage(examQueryDTO.getPageNum(), examQueryDTO.getPageSize());
            examVoList = baseMapper.selectUserExamList(userId);
            examCacheManager.refreshCache(ExamListType.USER_EXAM_LIST.getValue(), userId);
            total = new PageInfo<>(examVoList).getTotal();
        } else {
            examQueryDTO.setType(ExamListType.USER_EXAM_LIST.getValue());
            examVoList = examCacheManager.getExamVOList(examQueryDTO, userId);
            total = examCacheManager.getListSize(examQueryDTO.getType(), userId);
        }
        if (CollUtil.isEmpty(examVoList)) {
            return TableDataInfo.empty();
        }
        return TableDataInfo.success(examVoList, total);
    }
}
